// pages/weekly/weekly.js
const util = require('../../utils/util.js');
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userId: null,
    username: '',
    currDate: null,
    startDate: null,
    endDate: null,
    isShowModal: false,
    canAddReport: false,
    reportArr: [],
    page: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const currDate = util.formatDate(new Date());
    this.setData({
      currDate: currDate,
      userId: options.userId,
      canAddReport: options.userId==wx.getStorageSync('userId')
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.page = 0;
    this.data.reportArr = [];
    this.reqWeeklyReportsData();  // 根据用户 id 获取计划列表
  },

  /**
   * 下来刷新
   */
  onPullDownRefresh() {
    this.setData({
      page: 0,
      reportArr: []
    })
    this.reqWeeklyReportsData();  // 根据用户 id 获取计划列表
    wx.stopPullDownRefresh();  // 停止下拉刷新
  },

  /**
   * 上拉加载更多
   */
  onReachBottom() {
    this.reqWeeklyReportsData();  // 根据用户 id 获取计划列表
  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '社员计划'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '社员计划'
    }
  },

  /**
   * 请求获取计划数据
   */
  reqWeeklyReportsData() {

    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 30000,
      mask: true
    })

    request.GET('/weekly/report/pageList', {userId: this.data.userId, page: this.data.page}).then(res => {
      //console.log(res);
      if (res.data.resCode==200) {
        var page = this.data.page;
        var tempArr = this.data.reportArr;
        if (res.data.dataArrList.length!=0) {  // 每次请求分页的数量
          page++;
        }
        tempArr = tempArr.concat(res.data.dataArrList);
        
        this.setData({
          page: page,
          reportArr: tempArr,
          username: res.data.username
        })
        wx.hideToast();
      } else {
        wx.showToast({
          title: '获取数据异常',
          icon: 'error',
          duration: 1500
        })
      }
    })
  },

  /**
   * 隐藏 modal
   */
  clickHideModal() {
    this.setData({
      isShowModal: false
    })
  },

  /**
   * 点击新建计划
   */
  clickAddWeeklyReport() {
    this.setData({
      isShowModal: true
    })
  },

  /**
   * modal 日期选择修改
   */
  bindModalDateChange(e) {
    const dateKey = e.currentTarget.dataset.date;
    this.setData({
      [dateKey]: e.detail.value
    })
  },

  /**
   * 点击保存新建计划
   */
  clickModalSave() {

    if (this.data.startDate && this.data.endDate) {
      if (this.data.startDate < this.data.endDate) {
        wx.showToast({
          title: '新建中',
          icon: 'loading',
          duration: 30000,
          mask: true
        })

        const param = {
          userId: this.data.userId,
          startDate: this.data.startDate,
          endDate: this.data.endDate
        }
        request.GET('/weekly/report/save', param).then(res => {
          //console.log(res)
          if (res.data.resCode==200) {
            this.setData({
              isShowModal: false
            })
            wx.hideToast();
            
            const sd = (this.data.startDate).replace(/-/g, '.');
            const ed = (this.data.endDate).replace(/-/g, '.');
            wx.navigateTo({
              url: '/pages/weekly_detail/weekly_detail?id='+ res.data.weeklyInfo['id'] +'&userId='+ this.data.userId +'&startDate='+ sd +'&endDate='+ ed,
            })
          } else if (res.data.resCode==201) {
            wx.showToast({
              title: '该日期范围计划已存在',
              icon: 'none',
            })
          } else {
            wx.showToast({
              title: '计划创建异常',
              icon: 'error',
              mask: true
            })
          }
        })
      } else {
        wx.showToast({
          title: '开始时间必须小于结束时间',
          icon: 'none',
          mask: true
        })
      }
    } else {
      wx.showToast({
        title: '时间均不能为空',
        icon: 'none',
        mask: true
      })
    }
  },

  /**
   * 点击查看计划详情
   */
  viewReportDetail(e) {
    wx.navigateTo({
      url: '/pages/weekly_detail/weekly_detail?id='+ e.currentTarget.dataset.id +'&userId='+ this.data.userId +'&startDate='+ e.currentTarget.dataset.start +'&endDate='+ e.currentTarget.dataset.end,
    })
  },

  /**
   * 点击编辑计划
   */
  clickModifyWeekly(e) {
    wx.navigateTo({
      url: '/pages/weekly_detail/weekly_detail?id='+ e.currentTarget.dataset.id +'&userId='+ this.data.userId +'&startDate='+ e.currentTarget.dataset.start +'&endDate='+ e.currentTarget.dataset.end,
    })
  },

   /**
   * 点击删除计划
   */
  clickDelWeekly(e) {
    const that = this;
    wx.showModal({
      title: '温馨提示',
      content: '确定要删除该计划吗?',
      success (res) {
        if (res.confirm) {
          wx.showToast({
            title: '正在删除',
            icon: 'loading',
            duration: 30000,
            mask: true
          })

          request.GET('/weekly/delWorkReport', {id: e.currentTarget.dataset.id}).then(res => {
            // console.log(res)
            if (res.data.resCode==200) {
              wx.showToast({
                title: '已删除',
                icon: 'success',
                duration: 1000
              })
              that.onShow();
            } else {
              wx.showToast({
                title: '删除异常',
                icon: 'error',
                mask: true
              })
            }
          })
        } 
      }
    })
  },
  
})